﻿using System;
using System.Configuration;
using System.Data;

using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Data.SqlClient;
using System.IO;
using System.Xml;
/// <summary>
/// Summary description for Vid3
/// </summary>
public class Vid3
{
    public string[] VidT3(int noVids)
    {


        string ConnStr;
        ConnStr = ConfigurationManager.ConnectionStrings["connmTunez"].ToString();
        SqlConnection Conn = new SqlConnection(ConnStr);
        Conn.Open();

        SqlCommand chk = new SqlCommand("searchTop3", Conn);
        chk.CommandType = CommandType.StoredProcedure;
        SqlDataReader rdr = chk.ExecuteReader();
        string[] vids = new string[noVids];
        string[] title = new string[noVids];
        string[] concat = new string[noVids];
        int i = 0;
        while (rdr.Read())
        {
            vids[i] = rdr["VideoID"].ToString();
            vids[i] = vids[i] + ',';
            title[i] = rdr["title_"].ToString();
            concat[i] = string.Concat(vids[i], title[i]);
            i = i + 1;
        }
        rdr.Close();
        Conn.Close();
        return concat;
    }

    public string rtspURL(string vid)
    {
        XmlTextReader reader = new XmlTextReader("http://gdata.youtube.com/feeds/api/videos/" + vid);//&format=1 HD &format=6 LD  http://gdata.youtube.com/feeds/api/videos/jigeKOlJA50?v=2
        XmlDocument document = new XmlDocument();
        document.Load(reader);
        XmlNodeList content = document.GetElementsByTagName("media:content");
        //  Response.Write(content.Item(1).Attributes[0].Value);
        string urlSD = content.Item(1).Attributes[0].Value;
        string urlHD = content.Item(2).Attributes[0].Value;
        return urlSD + "," + urlHD;
    }

    public string[] Rating(string vid)
    {
        string[] returnData = new string[10];


        XmlTextReader reader = new XmlTextReader("http://gdata.youtube.com/feeds/api/videos/" + vid + "?v=2"); //http://gdata.youtube.com/feeds/api/videos/jigeKOlJA50?v=2
            XmlDocument document = new XmlDocument();
            document.Load(reader);
            XmlNodeList ratingContent=document.GetElementsByTagName("gd:rating");
            XmlNodeList viewCount=document.GetElementsByTagName("yt:statistics");
            XmlNodeList duration=document.GetElementsByTagName("yt:duration");
            XmlNodeList published = document.GetElementsByTagName("published");

            if (ratingContent.Count >0 )
            {            
                returnData[0] = ratingContent.Item(0).Attributes[0].Value;
                returnData[0] = returnData[0].Substring(0, 3);
            }
            else
                returnData[0] = "000000";
            if (viewCount.Item(0).Attributes[1].Value != null)
            {               
                returnData[1] = viewCount.Item(0).Attributes[1].Value;
            }
            else
                returnData[1] = "";
            if (duration.Item(0).Attributes[0].Value != null)
            {
                returnData[2] = duration.Item(0).Attributes[0].Value;
                returnData[2]= string.Format("{0:0.00}",Convert.ToDouble(duration.Item(0).Attributes[0].Value) / 60.00);// String.Format("{0:0.0}", dec)
            }
            else
                returnData[2] = "";
            if (published[0].InnerText != null)
            {
                returnData[3] = published[0].InnerText;
                returnData[3] = returnData[3].Substring(0, 10);
                returnData[3] = (DateTime.Now.Subtract( Convert.ToDateTime(returnData[3])).Days).ToString();
            
            }

            return returnData;
        
    }

    public string[] emailUpload()
    {
        XmlTextReader reader = new XmlTextReader("http://gdata.youtube.com/feeds/api/users/mobiletunez/uploads?start-index=1&amp;max-results=25&amp;orderby=updated");
        XmlDocument document = new XmlDocument();
        document.Load(reader);
        XmlNodeList vid = document.GetElementsByTagName("media:content");
        XmlNodeList vtitle = document.GetElementsByTagName("media:title");
        string[] id = new string[30];
        string[] title = new string[30];
        string[] urlSD = new string[30];
        string[] urlHD = new string[30];
        for (int i = 0; i < 2; i++)
        {
            try
            {
                int a3 = i * 3;
                id[i] = vid.Item(a3).Attributes[0].Value;
                title[i] = vtitle.Item(i).InnerText;
                id[i] = id[i].Substring(id[i].IndexOf("v/") + 2, (id[i].IndexOf("?") - id[i].IndexOf("v/")) - 2);

            }
            catch
            {
                break;
            }
        }

        string ConnStr = "";
        ConnStr = ConfigurationManager.ConnectionStrings["connmTunez"].ToString();
        SqlConnection Conn = new SqlConnection(ConnStr);
        Conn.Open();

        SqlCommand chk = new SqlCommand();
        chk.Connection = Conn;
        chk.CommandText = "checkVid";
        for (int i = 0; i < 25; i++)
        {
            if (id[i] != null)
            {
                chk.Parameters.Clear();
                chk.Parameters.AddWithValue("@videoID", id[i]);
                chk.CommandType = System.Data.CommandType.StoredProcedure;

                if (chk.ExecuteScalar() == null)
                {
                    chk.Parameters.Clear();
                    chk.CommandText = "saveRecord";
                    chk.Parameters.AddWithValue("@videoID", id[i]);
                    chk.Parameters.AddWithValue("@title", title[i]);
                    chk.Parameters.AddWithValue("@date", Convert.ToDateTime(DateTime.UtcNow));
                    chk.Parameters.AddWithValue("@mainTag", "College tunez");
                    chk.Parameters.AddWithValue("@subTag", "Music");
                    chk.Parameters.AddWithValue("@rtspHQ", "NA");
                    chk.Parameters.AddWithValue("@rtspNormal", "NA");
                    chk.CommandType = System.Data.CommandType.StoredProcedure;
                    chk.ExecuteNonQuery();

                }


            }

        }

        return id;
    }
}